package com.apes.scm.account.ledger.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.account.ledger.model.Ledger;
import com.apes.scm.account.ledger.model.LedgerAccount;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 * @author xul
 * @create 2018-11-03 10:25
 */
public interface LedgerRepository extends CustomRepository<Ledger, String> {

    @Query("select T " +
            "  from Ledger T  " +
            " where T.company = :company  " +
            "   and T.dept = :dept  " +
            "   and T.partner = :partner  " +
            "   and T.inventoryMode = :inventoryMode  " +
            "   and T.accountingMethod = :accountingMethod  " +
            "   and T.ledgerAccount = :ledgerAccount")
    Ledger findLedger(@Param("company") PartyRole company, @Param("dept") Dept dept,
                      @Param("partner") PartyRole partner, @Param("inventoryMode") String inventoryMode,
                      @Param("accountingMethod") String accountingMethod, @Param("ledgerAccount") LedgerAccount ledgerAccount);

    @Query("select T " +
            "  from Ledger T  " +
            " where T.company = :company  " +
            "   and T.partner = :partner  " +
            "   and T.inventoryMode = :inventoryMode  " +
            "   and T.accountingMethod = :accountingMethod  " +
            "   and T.ledgerAccount = :ledgerAccount")
    List<Ledger> findLedger(@Param("company") PartyRole company,
                            @Param("partner") PartyRole partner, @Param("inventoryMode") String inventoryMode,
                            @Param("accountingMethod") String accountingMethod, @Param("ledgerAccount") LedgerAccount ledgerAccount);

    @Query("select T " +
            "  from Ledger T  " +
            " where T.company = :company  " +
            "   and T.partner = :partner  " +
            "   and T.inventoryMode = :inventoryMode  " +
            "   and T.ledgerAccount.id = :ledgerAccountId and T.createDate <=:billingPeriod ")
    List<Ledger> findLedger(@Param("company") PartyRole company,
                            @Param("partner") PartyRole partner, @Param("inventoryMode") String inventoryMode,
                            @Param("ledgerAccountId") String ledgerAccountId,@Param("billingPeriod") Date billingPeriod);
}
